const express = require("express");
const request = require("request");
const template_id = "GyRcWwl9Iu34NxKyPlEE7QeZLveOmPxwAd1I6g6Pe0M";
// const touser = "oOSlA52A7eJKV2jJEK4MkSzd5_5s";
const app = express();

app.get("/", async (req, res) => {
  console.log("req.query", req.query);
  console.log("req.params", req.params);
  const {
    touser = "oOSlA52A7eJKV2jJEK4MkSzd5_5s",
    yname = "hzb",
    date = "0",
    words = "今日竟没有情话，请联系管理员！",
    weather = "今日竟没有天气，请联系管理员！",
  } = req.query;
  try {
    request(
      {
        method: "POST",
        url: `http://api.weixin.qq.com/cgi-bin/message/template/send`, // 这里就是少了一个token
        body: JSON.stringify({
          touser,
          template_id,
          data: {
            first: {
              value: "爱情每日提醒~",
              color: "#CD5C5C",
            },
            keyword1: {
              value: `亲爱的${yname}`,
              color: "#FAF744",
            },
            keyword2: {
              value: `今日天气：${weather}`,
              color: "#26A9BF",
            },
            keyword3: {
              value: `今天是恋爱的第${date}天💑`,
              color: "#C48CF5",
            },
            remark: {
              value: `今日情话：${words}`,
              color: "#F6B024",
            },
          },
        }),
      },
      function (error, response) {
        // console.log("接口返回内容", response);
        console.log(error);
        res.send("hhhhhh" + JSON.stringify(response));
        // resolve(JSON.parse(response.body));
      }
    );
  } catch (error) {
    console.log(error);
  }
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log("服务启动成功，端口：", port);
});
